package com.team.mp.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.team.mp.api.bo.ServicePackBo;
import com.team.mp.api.service.intf.ServicePackService;
import com.team.mp.mapper.dao.*;
import com.team.mp.mapper.dto.ServicePackDto;
import com.team.mp.mapper.entity.*;
import com.team.mp.vo.R;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author lee
* @description 针对表【t_service_pack(服务包)】的数据库操作Service实现
* @createDate 2023-03-27 16:44:20
*/
@Service
public class ServicePackServiceImpl implements ServicePackService {

    @Resource
    private ServicePackDao dao;
    @Resource
    private ServicePackDetailsDao detailsDao;

    @Resource
    private PackageTagDao tagDao;

    @Resource
    private SpackItemDao spackItemDao;
    @Override
    public R servicePackList() {
        return R.ok(dao.selectList(null));
    }

    @Override
    public R servicePackDetail(Integer id) {
        ServicePackDto spd = new ServicePackDto();
        // 拼接 servicePack
        ServicePack sp = dao.selectOne(new QueryWrapper<ServicePack>().eq("id", id));
        BeanUtils.copyProperties(sp,spd);
        // 拼接 lable
        spd.setServiceManageLable(serviceLabels(id));
        // 拼接 serviceItems
        spd.setServiceItemsList(serviceItems(id));
        spd.setServicePackDetails(detailsDao.selectOne(new QueryWrapper<ServicePackDetails>()
                .eq("spid",id)));
        return R.ok(spd);
    }

    @Override
    public R updateAuditStatus(Integer status, Integer id) {
        int ret = dao.update(new ServicePack(status), new QueryWrapper<ServicePack>()
                .eq("id", id));
        return ret > 0 ? R.ok() : R.fail("更新状态失败");
    }

    @Override
    public R selectedItems(Integer id) {
        List<ServiceItems> serviceItems = dao.querySelectedItems(id);
        return R.ok(serviceItems);
    }

    @Override
    public R addItems(List<Integer> itemIds, Integer id) {
        if (itemIds != null && id > 0){
            int i = dao.insertBatchItems(itemIds, id);
            return i > 0 ? R.ok() : R.fail();
        }
        return R.fail("为服务包新增服务项目失败");
    }

    @Transactional
    @Override
    public R add(ServicePackBo bo) {
        System.out.println(bo);
        if (bo != null){
            // 新增服务包
            ServicePack servicePack = new ServicePack();
            BeanUtils.copyProperties(bo, servicePack);
            servicePack.setCreateTime(new Date());
            int insert = dao.insert(servicePack);
            if (insert > 0){
                // 新增 服务标签
                List<Integer> labels = bo.getServiceManageLable();
                int insertBatchTag = tagDao.insertBatchTag(labels, servicePack.getId());
                if (insertBatchTag > 0){
                    // 新增 服务详情
                    ServicePackDetails packDetails = new ServicePackDetails();
                    BeanUtils.copyProperties(bo.getServicePackDetails(), packDetails);
                    packDetails.setSpid(servicePack.getId());
                    packDetails.setCreateTime(new Date());
                    int insert1 = detailsDao.insert(packDetails);
                    if (insert1 > 0){
                        return R.ok();
                    }
                }
            }
        }
        return R.fail();
    }

    @Override
    public R delete(Integer id) {
        if (id > 0){
            // 删除服务包
            dao.deleteById(id);
            // 删除服务包和标签的关系
            tagDao.delete(new LambdaQueryWrapper<PackageTag>().eq(PackageTag::getPid, id));
            // 删除服务包和服务项目的关系
            spackItemDao.delete(new LambdaQueryWrapper<SpackItem>().eq(SpackItem::getSpid, id));
            // 删除服务包对应的详情
            detailsDao.deleteById(new LambdaQueryWrapper<ServicePackDetails>().eq(ServicePackDetails::getSpid, id));
        }
        return R.fail();
    }


    /**
     * 服务包标签表
     * @param id
     * @return
     */
    private List<String> serviceLabels(Integer id){
        return dao.selectLabels(id);
    }

    /**
     * 服务包对应项目表
     * @param id
     * @return
     */
    public List<ServiceItems> serviceItems(Integer id) {
        return dao.selectItems(id);
    }
}




